class Solution {
public:
    int monotoneIncreasingDigits(int n) {
        string s = to_string(n);
        int index = s.size();
        for(int i = s.size() - 2;i >= 0;i--){
            if(s[i + 1] < s[i]){
                s[i + 1] = '9';
                s[i] -= 1;
                index = i;
            }
        }
        for(int i = index + 1;i < s.size();i++){
                s[i] = '9';
        }
         n = atoi(s.c_str());
        return n;
    }
};
